package com.yoomiito.app.ui.anyview.gasstation

import android.content.Context
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import android.view.View
import android.widget.TextView
import com.scwang.smartrefresh.layout.SmartRefreshLayout
import com.yoomiito.app.R
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.widget.SpacingDecoration
import com.yoomiito.app.widget.state.*


class RcyView(context: Context, refresh: Boolean,emptyContent:String) {
    private var rcy: androidx.recyclerview.widget.RecyclerView
    private var smartRefreshLayout:SmartRefreshLayout
    var onLoadMoreListener: () -> Unit = {}
    var onRetryListener:()->Unit={}
    val mStatusManager: StatusManager = StatusManager.Builder(context)
            .addType(ContentType(R.layout.fm_rcy))
            .addType(EmptyType_3().setEmptyContent(emptyContent))
            .addType(ErrorType().setMarginTop(200).setClickListener {
                onRetryListener()
            })
            .build()

    init {
        rcy = mStatusManager.view().findViewById(R.id.fm_rcy)
        rcy.addItemDecoration(SpacingDecoration(0, DensityUtil.dp2px(5f), false))
        rcy.layoutManager = androidx.recyclerview.widget.LinearLayoutManager(context)
        smartRefreshLayout = mStatusManager.view().findViewById(R.id.fm_refreshLayout)
        smartRefreshLayout.setEnableRefresh(false)
        smartRefreshLayout.setEnableLoadMore(refresh)
        smartRefreshLayout.setOnLoadMoreListener {
            onLoadMoreListener()
        }
    }

    fun getView(): LoadLayout = mStatusManager.view()

    fun setAdapter(adapter: androidx.recyclerview.widget.RecyclerView.Adapter<*>?) {
        if (adapter == null) {
            showEmpty(EmptyType_3::class.java)
        } else {
            rcy.adapter = adapter
            mStatusManager.showContent()
        }
    }

    fun scrollTop(){
        rcy.scrollToPosition(0)
    }

    fun show(show: Boolean, empty: Boolean) {
        mStatusManager.view().visibility = if (show) View.VISIBLE else View.GONE
        if (empty) showEmpty(EmptyType_3::class.java) else mStatusManager.showContent()
    }

    fun finishLoadMoreWithNoMoreData() {
        smartRefreshLayout.finishLoadMoreWithNoMoreData()
    }

    fun setNoMoreData(noMore:Boolean){
        smartRefreshLayout.setNoMoreData(noMore)
    }

    fun finishLoadMore(){
        smartRefreshLayout.finishLoadMore()
    }

    fun showEmpty(cls:Class<EmptyType_3>){
        mStatusManager.showEmpty(cls)
    }

    fun showContent(){
        mStatusManager.showContent()
    }

    fun showError(){
        mStatusManager.showError()
    }

}